package com.kelvin.backend.common.util.json;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.kelvin.backend.common.util.date.DatePattern;
import com.kelvin.backend.common.util.date.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;


/**
 * <p>Title: JSONUtil</p>
 * <p>Description: </p>
 *
 * @author Kelvin.Chen
 * @blame Lenovo
 * @datetime 2020/7/1 18:02
 */
public class JacksonUtil {

    private static final Logger log = LoggerFactory.getLogger(JacksonUtil.class);

    private final static ObjectMapper mapper;
    static {
        mapper = new ObjectMapper();
        // 忽略json中在对象不存在对应属性
        mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
        // 忽略bean转json错误
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    }

    /**
     * JSON字符串转Java对象
     *
     * @param json
     * @param classType
     * @param <T>
     * @return
     */
    public static <T> T toJavaObject(String json, Class<T> classType) {
        if (ObjectUtil.isEmpty(json)) {
            log.warn("The json string is empty!");
            return null;
        }

        try {
            return mapper.readValue(json, classType);
        } catch (JsonProcessingException e) {
            log.error("Json string parse to Java Object error!", e);
        }

        return null;
    }

    /**
     * JSON字符串转化为带泛型的对象
     *
     * @param json
     * @param typeReference
     * @param <T>
     * @return
     */
    public static <T> T toJavaObject(String json, TypeReference<T> typeReference) {
        if (ObjectUtil.isEmpty(json) || ObjectUtil.isEmpty(typeReference)) {
            log.warn("The json string is null or typeReference is null");
            return null;
        }

        try {
            return mapper.readValue(json, typeReference);
        } catch (JsonProcessingException e) {
            log.error("Json string parse to Java Object error!", e);
        }

        return null;
    }

    /**
     * 对象转JSON字符串
     *
     * @param obj
     * @return
     */
    public static String toJsonStr(Object obj) {
        if (obj instanceof String) {
            return (String) obj;
        }

        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error("The Object parse to json error!", e);
        }

        return null;
    }

    /**
     * 驼峰转下划线json字符串
     *
     * @param object
     * @return
     */
    public static String toSnakeCaseJsonString(Object object) throws JsonProcessingException {
        DateFormat dateFormat = DateUtil.getDateFormat(DatePattern.NORMAL_DATETIME_PATTERN);
        mapper.setDateFormat(dateFormat);
        mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return mapper.writeValueAsString(object);
    }

    /**
     * json下划线格式转驼峰对象
     *
     * @param json
     * @return
     */
    public static Object toCamelCaseObject(String json, Class valueType) throws JsonProcessingException {
        mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return mapper.readValue(json, valueType);
    }


}
